/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef __CUBEFS_CORE_BTREE_ITERATOR_MODEL_API_H
#define __CUBEFS_CORE_BTREE_ITERATOR_MODEL_API_H

#include <iostream>

#include <cubefs/api/types.hpp>
#include <cubefs/core/container/container.hpp>
#include <cubefs/core/btree/names.hpp>

#define CUBEFS_SOURCE "cubefs/core/btree/iterator/model_api.hpp:" CUBEFS_AT


namespace cubefs    {
namespace ctr       {

using namespace cubefs::btree;


CUBEFS_ITERATOR_PART_BEGIN(IteratorModelAPIName)

    typedef typename Base::NodeBase                                             NodeBase;
    typedef cubefs::api::Map                                                    Map;

    typedef typename Base::Model::ApiKeyType                                    ApiKeyType;
    typedef typename Base::Model::ApiValueType                                  ApiValueType;

    typedef typename Base::Model::Page                                          PageType;
    typedef typename Base::Model::ID                                            ID;

    static const Int PAGE_SIZE = Base::Model::PageMgr::PAGE_SIZE;

    virtual cubefs::api::Model* GetModel() {
        return &me_.model();
    }

    virtual bool Next() {
        return me_.NextKey();
    }

    virtual bool Prev() {
        return false;
    }
    
    virtual void Close() {}
    
    virtual bool IsClonable() {
        return true;
    }
    
    virtual cubefs::api::Iterator* Clone() {
        return new MyType(me_);
    }
    
    virtual void GetValue(Typed *holder) {
        if (holder == NULL) {
            throw NullPointerException(CUBEFS_SOURCE, "value holder can't be null");
        }

        if (holder->type() != Map::Pair::MyType()) {
            throw TypeCastException(CUBEFS_SOURCE, "value holder is not a Map::Pair", holder->type(), Map::Pair::MyType());
        }

        Map::Pair* pair = static_cast<Map::Pair*>(holder);

        pair->CheckKVNotNull();

        TypedValueHelper<typename ApiValueType::InterfaceType>::set(pair->value(), me_.GetData());

        StaticListOfTyped* keys = TypedCast<StaticListOfTyped>(pair->key());

        TypedValueHelper<typename ApiKeyType::InterfaceType>::set(keys->Get(pair->GetIdx()), me_.GetKey(0));
    }

    virtual cubefs::api::Page* GetPage() {
        if (me_.page() != NULL) {
            return new PageWrapper<PageType, PAGE_SIZE>(me_.page());
        }
        else {
            return NULL;
        }
    }

    virtual Int GetIndex() {
        return me_.key_idx();
    }

    virtual bool IsFlag(Int flag) {
        switch (flag) {
            case Base::ITER_EOF:    return me_.IsEof();
            case Base::ITER_EMPTY:  return me_.IsEmpty();
            case Base::ITER_BOF:    return me_.IsBof();
            default:                throw CubefsException(CUBEFS_SOURCE, "Invalig flag name", flag);
        }

    }
    
CUBEFS_ITERATOR_PART_END

}
}

#undef CUBEFS_SOURCE

#endif //__CUBEFS_CORE_BTREE_ITERATOR_MODEL_API_H
